package com.tourism.website.login.controller;

import com.tourism.website.apis.LoginApis;
import com.tourism.website.login.service.UserService;
import com.tourismwebsite.login.query.UserQuery;
import com.tourismwebsite.vo.JsonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "登录和注册管理模块")
public class UserController implements LoginApis {
    @Resource
    private UserService userService;
    @PostMapping("/query-login")
    @ApiOperation(value = "验证是否登录成功")
    public JsonVO<String> QueryUser(@Validated UserQuery userQuery){
        System.out.println(userQuery.getUsername()+","+userQuery.getPassword());
        String result = userService.QueryUser(userQuery);
        if(result.equals("用户名不存在")){
            return JsonVO.create(null,9999,result);
        }else if(result.equals("用户名或者密码错误")){
            return JsonVO.create(null,9999,result);
        }else{
            return JsonVO.create(null,10000,result);
        }
    }
    @PostMapping("/insert-register")
    @ApiOperation(value = "增加新用户")
    public JsonVO<String> InsertUser(UserQuery userQuery){
        System.out.println(userQuery.getUsername()+","+userQuery.getPassword());
        boolean result = userService.InsertUser(userQuery);
        if(result){
            return JsonVO.success(null);
        }
        return JsonVO.create(null,9999,"注册失败");
    }
}
